package org.syncsoft.overalt.gui.impl.componentPlugin;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JTable;

import org.syncsoft.overalt.gui.api.WindowContextValue;
import org.syncsoft.overalt.gui.api.componentPlugin.AbstractTableDataModelComponentPlugin;
import org.syncsoft.overalt.gui.impl.tableDataModel.StringTableDataModel;

/**
 * 
 * @author tptfc
 *
 */
public class TableStringComponentPlugin extends AbstractTableDataModelComponentPlugin<StringTableDataModel> {
	private Map<String, StringTableDataModel> dataModelIndex;

	{
		setDataModelIndex(new HashMap<String, StringTableDataModel>());
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.IComponentPlugin#buildContext(org.syncsoft.overalt.gui.swing.api.ContextWindowValue)
	 */
	@Override
	public void buildContext(WindowContextValue context) {
		for (String key : getDataModelIndex().keySet()) {
			StringTableDataModel dataModel = getDataModel(key);
			addSelectedDataInContext(key, context, dataModel);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractTableDataModelComponentPlugin#getDataModelValue(int, org.syncsoft.overalt.gui.swing.api.AbstractTableDataModel)
	 */
	@Override
	protected Object getDataModelValue(int row, StringTableDataModel dataModel) {
		return dataModel.getValue(row);
	}
	
	/**
	 * 
	 * @param key
	 * @return
	 */
	protected StringTableDataModel getDataModel(String key) {
		return getDataModelIndex().get(key);
	}
	
	/**
	 * 
	 * @param key
	 * @param dataModel
	 */
	protected void addValueDataModesl(String key, StringTableDataModel dataModel) {
		getDataModelIndex().put(key, dataModel);
	}
	
	/**
	 * 
	 * @param key
	 * @param columnsName
	 * @param values
	 * @return
	 */
	public JTable newTable(String key, Collection<String> columnsName, Collection<String> values) {
		return newTable(key, columnsName, values, null);
	}

	/**
	 * 
	 * @param key
	 * @param columnsName
	 * @param values
	 * @param selectedValues
	 * @return
	 */
	public JTable newTable(String key, Collection<String> columnsName, Collection<String> values, Collection<String> selectedValues) {
		JTable table;
		
		if (values != null) {
			StringTableDataModel dataModel = new StringTableDataModel(columnsName, values, selectedValues);
			table = new JTable(dataModel);
			addValueDataModesl(key, dataModel);
		} else {
			table = new JTable(null, columnsName.toArray());
		}

		table.getColumnModel().getColumn(0).setMaxWidth(30);
		
		return table;
	}

	
	//	GETs AND SETs
	private Map<String, StringTableDataModel> getDataModelIndex() {
		return dataModelIndex;
	}
	private void setDataModelIndex(Map<String, StringTableDataModel> dataModelIndex) {
		this.dataModelIndex = dataModelIndex;
	}
}
